package com.cssw.fyzb.busi.service.exchange;

import com.cssw.fyzb.core.dao.user.UserDao;
import com.cssw.fyzb.core.eao.acct.AcctLogEao;
import com.cssw.fyzb.core.model.vo.acct.AcctGlobalVo;
import com.cssw.fyzb.busi.service.acct.AcctService;
import com.cssw.fyzb.busi.service.acct.AcctServiceImpl;
import com.cssw.fyzb.core.dao.exchange.GExchangeFDefDao;
import com.cssw.fyzb.core.dao.exchange.GExchangeFLogDao;
import com.cssw.fyzb.core.dao.system.SysParamDao;
import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.core.util.AesUtil;
import com.cssw.fyzb.core.util.BizUtil;
import com.cssw.fyzb.core.util.StringUtil;
import com.ydn.dbframe.plugin.activerecord.Db;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.concurrent.atomic.AtomicReference;


@Repository
public class GExchangeFServiceImpl implements GExchangeFService {
    private static final Logger logger = LoggerFactory.getLogger(AcctServiceImpl.class);
    private static final int DEF_DIV_SCALE = 7;
    @Autowired
    private AcctService acctService;
    @Autowired
    private SysParamDao sysParamDao;
    @Autowired
    private GExchangeFLogDao gExchangeFLogDao;
    @Autowired
    private GExchangeFDefDao gExchangeFDefDao;
    @Autowired
    private AcctLogEao acctLogEao;
    @Autowired
    private UserDao userDao;

    @Override
    public String gExchangeFDef(BigDecimal num, Long userId, Long logId, Integer type, String pwd) {
        User user = userDao.getById(userId);
        if (StringUtil.isEmpty(user.getTradePwd())) {
            BizUtil.exception("您还未设置交易密码");
        }
        if (StringUtil.isEmpty(pwd)) {
            BizUtil.exception("请输入密码");
        }
        if (!AesUtil.encode(pwd).equals(user.getTradePwd())) {
            BizUtil.exception("交易密码错误");
        }

        AtomicReference<BigDecimal> returnNum = new AtomicReference<>();
        boolean flag = Db.tx(() -> {


            BigDecimal gNum = null;
            BigDecimal fValue = null;
            AcctGlobalVo acctGlobalVo = acctService.queryAcctGlobal(userId);
            SysParam sysParam = sysParamDao.findByCode("G_EXCHANGE_F_RATIO");

            if (type == 1) {
                if (acctGlobalVo.getGBalance() == null) BizUtil.exception("您的果币为空，请确认后在操作");
                gNum = acctGlobalVo.getGBalance();
                fValue = gNum.multiply(new BigDecimal(sysParam.getParamValue()));
            } else if (type == 2) {
                GExchangeFDef gExchangeFDef = gExchangeFDefDao.getGExchangeFDef(logId);
                if (gExchangeFDef.getGCoin() == null) BizUtil.exception("您的果子为空，请确认后在操作");
                if (gExchangeFDef.getGCoin().compareTo(acctGlobalVo.getGBalance()) >= 1) BizUtil.exception("您的果子大于果币");
                gNum = gExchangeFDef.getGCoin();
                fValue = gExchangeFDef.getFCoin();
            } else {
                if (num.compareTo(acctGlobalVo.getGBalance()) >= 1) BizUtil.exception("您的兑换金额有误，请确认后在操作");
                if (sysParam.getParamValue() == null) BizUtil.exception("参数为空，请稍后操作");
                gNum = num;
                fValue = num.multiply(new BigDecimal(sysParam.getParamValue()));
            }
            Long id = gExchangeFLogDao.updateData(userId, gNum, fValue);
            acctService.addBalance(userId, "F", fValue, "" + id,
                    AcctTradeType.EXCHANGE_ADD, BusiType.EXCHANGE_CODE, "果子兑换福币");
            acctService.minusBalance(userId, "G", gNum, "" + id,
                    AcctTradeType.EXCHANGE_REDUCE, BusiType.EXCHANGE_CODE, "果子兑换福币");
//            acctService.updateData(userId, fValue.intValue());
//            //福币
//            acctLogEao.save(UuidUtil.get(), userId, userId, AcctType.ACCT_TYPE_F, fValue,
//                    Acct.FEE_IN_DE_ADD, userId.toString(), AcctTradeType.SETTLEMENT_REDUCE_F, AcctTradeType.SETTLEMENT_REDUCE_NAME_F,
//                    BusiType.Y_EXCHANDE_F_F, new Date());
            returnNum.set(fValue.setScale(2, BigDecimal.ROUND_DOWN));
            return true;
        });
        return returnNum.toString();
    }
}
